﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

namespace FinalProject
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                //rdbNuocKhac.AutoPostBack = true;                
                //rdbVietNam.AutoPostBack = true;
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
            }
            else
            {
                calCalender.SelectionChanged += new EventHandler(calCalender_SelectionChanged);
            }
        }

        void calCalender_SelectionChanged(object sender, EventArgs e)
        {
            tbxNgaySinh.Text = calCalender.SelectedDate.ToShortDateString();
        }        

        #region Check Validation
        private bool CheckEmail(string email)
        {
            if (email.Length < 4)
            {
                return false;
            }
            else
            {
                if (email.IndexOf(' ') > 0)
                    return false;

                if (email.IndexOf('@') <= 0 || email.IndexOf('@') != email.LastIndexOf('@'))
                    return false;

                if (email.Contains("..") || email.Contains("@.") || email.Contains(".@"))
                    return false;

                if (email[0] == '.' || email[email.Length - 1] == '.')
                    return false;

                if (email.IndexOf('@') > email.LastIndexOf('.'))
                    return false;

                string str = "qwertyuiopasdfghjklzxcvbnm-_.@";
                for (int i = 0; i < email.Length; i++)
                {
                    if (!str.Contains(email[i]))
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        private bool CheckPassword(string pass)
        {
            if (pass.Length < 4)
                return false;

            if (pass.Contains(' '))
                return false;

            return true;
        }

        private bool CheckHoTen(string hoTen)
        {
            if (hoTen == "")
                return false;

            if (hoTen[0] == ' ' || hoTen[hoTen.Length - 1] == ' ')
                return false;

            if (hoTen.Contains('"') || hoTen.Contains('|') || hoTen.Contains('\\') || hoTen.Contains('\n') || hoTen.Contains('\r'))
                return false;

            string str = "`~1!2@3#4$5%6^7&8*9(0)-_=+/?.>,<';:[{]}";
            for (int i = 0; i < str.Length; i++)
            {
                if (hoTen.Contains(str[i]))
                    return false;
            }

            return true;
        }

        private bool CheckValidation()
        {
            bool isValidately = true;

            #region Check Email
            if (CheckEmail(tbxEmailDangNhap.Text))
            {
                lblAlertEmailDangNhap.Visible = false;
                if (tbxEmailDangNhap.Text != tbxEmailXacNhan.Text)
                {
                    lblAlertEmailXacNhan.Visible = true;
                    isValidately = false;
                }
                else
                {
                    lblAlertEmailXacNhan.Visible = false;
                }
            }
            else
            {
                lblAlertEmailDangNhap.Visible = true;
                lblAlertEmailXacNhan.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Password
            if (CheckPassword(tbxMatKhau.Text))
            {
                lblAlertMatKhau.Visible = false;
                if (tbxMatKhau.Text.CompareTo(tbxMatKhauXacNhan.Text)!=0)
                {
                    lblAlertMatKhauXacNhan.Visible = true;
                    isValidately = false;
                }
                else
                {
                    lblAlertMatKhauXacNhan.Visible = false;
                }
            }
            else
            {
                lblAlertMatKhau.Visible = true;
                lblAlertMatKhauXacNhan.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Ho
            if (CheckHoTen(tbxHo.Text))
            {
                lblAlertHo.Visible = false;
            }
            else
            {
                lblAlertHo.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Ten
            if (CheckHoTen(tbxTen.Text))
            {
                lbtAlertTen.Visible = false;
            }
            else
            {
                lbtAlertTen.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Ngay thang nam sinh
            try
            {
                DateTime date = Convert.ToDateTime(Date1.Text);
                lblAlertNgaySinh.Visible = false;
            }
            catch
            {
                lblAlertNgaySinh.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Gioi tinh
            if (rdbNam.Checked != rdbNu.Checked)
            {
                lblAlertGioiTinh.Visible = false;
            }
            else
            {
                lblAlertGioiTinh.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Noi cu ngu
            if (ddlTinhThanh.SelectedIndex <= 0)
            {
                lblAlertTinhThanh.Visible = true;
                isValidately = false;
            }
            else
            {
                lblAlertTinhThanh.Visible = false;                
            }
            #endregion

            #region Check Dong y
            if (ckbDongY.Checked)
            {
                lblAlertDongY.Visible = false;
            }
            else
            {
                lblAlertDongY.Visible = true;
                isValidately = false;
            }
            #endregion
            return isValidately;
        }

        #endregion

        //Thêm user vào cơ sở dữ liệu
        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            
            if (CheckValidation())
            {                                
                int iGioiTinh = rdbNam.Checked?1:0;
                DateTime dtimeBirthday = DateTime.Parse(Date1.Text);
              
                //Utility.User = new UserInformation(
                //    tbxEmailDangNhap.Text, tbxMatKhau.Text, tbxHo.Text,
                //    tbxTen.Text, dtimeBirthday, iGioiTinh, Utility.LstTinhThanh[ddlTinhThanh.SelectedIndex - 1].ID
                //    );

                Utility.UserCurrent = new UserInformation(
                    tbxEmailDangNhap.Text, tbxMatKhau.Text, tbxHo.Text,
                    tbxTen.Text, dtimeBirthday, iGioiTinh, rdbVietNam.Checked, 
                    rdbVietNam.Checked?Utility.LstTinhThanh[ddlTinhThanh.SelectedIndex - 1].ID:Utility.LstQuocGia[ddlTinhThanh.SelectedIndex-1].ID
                    );

                int i = Utility.UserCurrent.insertUser();
                if (i == 1)
                {
                    lblAlertEmailDangNhap.Text = "Email này đã tồn tại!";
                    lblAlertEmailDangNhap.Visible = true;
                    tbxEmailXacNhan.Text = "";
                    return;
                }
                //Session["username"] = tbxEmailDangNhap.Text.Substring(0, tbxEmailDangNhap.Text.IndexOf('@'));
                Session["username"] = tbxEmailDangNhap.Text;
                Session["password"] = tbxMatKhau.Text;

                //Được khi không có redict
                //string script = "<script type=\"text/javascript\">alert(\"Đăng kí thành công!\");</script>";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", script);
                
                //Được khi không có redict
                //System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Hello this is an Alert\")</SCRIPT>");

                //Được khi không có redict
                //lblJavaScript.Text = "<script type=\"text/javascript\">displayMessege();</script>";

                //Response.Redirect("Home.aspx");

                //string helloUser = tbxEmailDangNhap.Text.Substring(0, tbxEmailDangNhap.Text.IndexOf('@'));
                string helloUser = tbxEmailDangNhap.Text;
                
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Chúc mừng bạn " + helloUser + " đã đăng kí thành công!\"); window.location = \"Home.aspx\"; </SCRIPT>");        
            }                 
        }

        protected void rdbNuocKhac_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNuocKhac.Checked)
            {
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstQuocGia);
            }
            else
            {
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
            }
        }

        protected void rdbVietNam_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbVietNam.Checked)
            {
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
            }
            else
            {
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstQuocGia);
            }
        }
    }
}
